package com.lwj.liwanjia.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lwj.liwanjia.R;
import com.lwj.liwanjia.bean.DpSpBean;
import com.superrecycleview.superlibrary.adapter.BaseViewHolder;
import com.superrecycleview.superlibrary.adapter.SuperBaseAdapter;

import java.util.List;

/**
 * Created by Administrator on 2017/9/19.
 */

public class DianPuAdapter extends SuperBaseAdapter<DpSpBean.DataBean> {
    private Context mContext;

    public DianPuAdapter(Context context, List<DpSpBean.DataBean> data) {
        super(context, data);
        mContext = context;
    }

    @Override
    protected void convert(BaseViewHolder holder, DpSpBean.DataBean item, int position) {
        Glide.with(mContext).load(item.getImageUrl()).placeholder(R.mipmap.station).error(R.mipmap.station).into((ImageView) holder.getView(R.id.image_shangping));
        String belong = item.getCommodityBelong();
        if (!TextUtils.isEmpty(belong)) {
            SpannableStringBuilder str = setTextView(belong, item.getCommodityName());
            holder.setText(R.id.tv_jianjie, str)
                    .setText(R.id.tv_lable, belong)
                    .setVisible(R.id.tv_lable, true);
        } else {
            holder.setText(R.id.tv_jianjie, item.getCommodityName())
                    .setVisible(R.id.tv_lable, false);
        }
        holder.setText(R.id.tv_current_price, "￥" + item.getNowPrice())
                .setText(R.id.tv_price, "￥" + item.getCommodityPrice());
        TextView tvPrice = holder.getView(R.id.tv_price);
        tvPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
    }

    @Override
    protected int getItemViewLayoutId(int position, DpSpBean.DataBean item) {
        return R.layout.promotion_more_item;
    }

    /**
     * 设置标签的颜色
     *
     * @param lable
     * @param str
     * @return
     */
    private SpannableStringBuilder setTextView(String lable, String str) {
        String string = lable + str;
        SpannableStringBuilder style = new SpannableStringBuilder(string);
        style.setSpan(new BackgroundColorSpan(mContext.getResources().getColor(R.color.colorWite)), 0, lable.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        style.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.color.colorWite)), 0, lable.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return style;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            final GridLayoutManager manager = (GridLayoutManager) layoutManager;
            manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    int type = getItemViewType(position - 1);
                    return (type == VIEW_TYPE.HEADER || type == VIEW_TYPE.FOOTER) ? manager.getSpanCount() : 1;
                }
            });
        }
    }
}
